﻿using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraNavBar;
using System;
using System.Drawing;

namespace DevTeam
{
    public class RibbonManager
    {
        #region Ribbon Manager

        public delegate void MenuClickedHandler(Type type, params object[] parameters);
        public event MenuClickedHandler MenuClicked;
        private NavBarControl easyNavBar = new NavBarControl();
        //public TreeList EasyNavBar
        //{
        //    get { return easyNavBar; }
        //    set { easyNavBar = value; }
        //}

        public RibbonControl RibbonControl { get; set; }

        public RibbonManager(RibbonControl ribbonControl)
        {
            RibbonControl = ribbonControl;
            RibbonControl.TransparentEditors =
            RibbonControl.Manager.TransparentEditors = true;
        }

        public Page AddPage(RibbonControl Ribbon, string Caption)
        {
            RibbonPage ribbonPage = new RibbonPage(Caption.ToUpper());
            Page page = new Page(this, ribbonPage);

            NavBarGroup navGroup = new NavBarGroup(Caption.ToUpper());

            if (Caption == "Ana Sayfa")
            {
                Ribbon.Pages.Insert(0, ribbonPage);
                Ribbon.SelectedPage = ribbonPage;
                //EasyNavBar.Groups.Insert(0, navGroup);
            }
            else
            {
                Ribbon.Pages.Add(ribbonPage);
                //EasyNavBar.Groups.Add(navGroup);
            }

            return page;
        }



        public class Page
        {
            private RibbonManager parent;
            public RibbonManager Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private RibbonPage ribbonPage;

            public RibbonPage RibbonPage
            {
                get { return ribbonPage; }
                set { ribbonPage = value; }
            }

            public Page(RibbonManager parent, RibbonPage ribbonPage)
            {
                this.RibbonPage = ribbonPage;
                this.Parent = parent;
            }

            public Group AddGroup(string Caption)
            {
                RibbonPageGroup ribbonPageGroup = new RibbonPageGroup(Caption);
                RibbonPage.Groups.Add(ribbonPageGroup);
                ribbonPageGroup.ShowCaptionButton = false;
                return new Group(this, ribbonPageGroup);
            }

            public Page SetVisibleFor(int yetkiId)
            {
                bool allowed = Session.CurrentUser.YetkiliMi(yetkiId);

                this.RibbonPage.Visible = allowed;
                return this;
            }
        }

        public class Group
        {
            private Page parent;
            public Page Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private RibbonPageGroup ribbonPageGroup;

            public RibbonPageGroup RibbonPageGroup
            {
                get { return ribbonPageGroup; }
                set { ribbonPageGroup = value; }
            }

            public Group(Page parent, RibbonPageGroup RibbonPageGroup)
            {
                this.RibbonPageGroup = RibbonPageGroup;
                this.Parent = parent;
            }

            public Menu AddMenu(string Caption, Bitmap icon)
            {
                BarButtonItem bbi = new BarButtonItem();
                bbi.Caption = Caption;
                bbi.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.Large;
                bbi.Glyph = icon;
                //bbi.AllowHtmlText = DevExpress.Utils.DefaultBoolean.True;
                //bbi.LargeGlyph = icon;

                this.RibbonPageGroup.ItemLinks.Add(bbi);
                return new Menu(this, bbi);
            }

            public Menu AddMenuGLE(string Caption, Bitmap icon)
            {
                BarEditItem bbi = new BarEditItem();
                bbi.Edit = new RepositoryItemGridLookUpEdit();
                bbi.Caption = Caption;
                bbi.PaintStyle = BarItemPaintStyle.CaptionGlyph;

                //DataTable dtx = ExecuteDataTable("GetKonfVendors");
                //RepositoryItemGridLookUpEdit gle = GetCustomGLE(dtx, "Name", "VendorId");
                //gleUretici.Edit.Assign(gle);

                //((RepositoryItemGridLookUpEdit)bbi.Edit) = RI(Rp.Brand);
                //bbi.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.Large;
                //bbi.Glyph = icon;
                this.RibbonPageGroup.ItemLinks.Add(bbi);
                return new Menu(this, bbi);
            }

            public Menu AddMenu(string Caption, Bitmap icon, Type type, params object[] parameters)
            {
                Menu menu = AddMenu(Caption, icon);

                menu.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    if (this.Parent.Parent.MenuClicked != null)
                        this.Parent.Parent.MenuClicked(type, parameters);
                };

                return new Menu(this, menu.BarButtonItem);
            }
        }

        public class Menu
        {
            private Group parent;
            public Group Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private BarButtonItem barButtonItem;
            public BarButtonItem BarButtonItem
            {
                get { return barButtonItem; }
                set { barButtonItem = value; }
            }

            private BarEditItem barEditItem;
            public BarEditItem BarEditItem
            {
                get { return barEditItem; }
                set { barEditItem = value; }
            }

            public Menu(Group parent, BarButtonItem barButtonItem)
            {
                this.BarButtonItem = barButtonItem;
                this.Parent = parent;
            }

            public Menu(Group parent, BarEditItem barEditItem)
            {
                this.BarEditItem = barEditItem;
                this.Parent = parent;
            }

            public void AddCustomAction(Action Method)
            {
                this.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    Method();
                };
            }

            public SubMenu AddSubMenu(bool BeginGroup, string Caption, Image icon, Type type, params object[] parameters)
            {
                SubMenu sm = AddSubMenu(Caption, icon, type, parameters);

                foreach (BarItemLink bi in sm.BarButtonItem.Links)
                    if (bi.Item == sm.BarButtonItem)
                    {
                        bi.BeginGroup = true;
                        break;
                    }

                return sm;
            }

            public SubMenu AddSubMenu(string Caption, Image icon, Type type, params object[] parameters)
            {
                SubMenu subMenu = AddSubMenu(Caption, icon);

                subMenu.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    if (this.Parent.Parent.Parent.MenuClicked != null)
                        this.Parent.Parent.Parent.MenuClicked(type, parameters);
                };

                return new SubMenu(this, subMenu.BarButtonItem);
            }

            public SubMenu AddSubMenu(string Caption, Image icon)
            {
                BarButtonItem.ButtonStyle = DevExpress.XtraBars.BarButtonStyle.DropDown;
                BarButtonItem.ActAsDropDown = true;
                BarButtonItem.AllowDrawArrow = true;
                BarButtonItem.ItemAppearance.SetWordWrap(DevExpress.Utils.WordWrap.NoWrap);

                PopupMenu popupMenu = null;
                if (BarButtonItem.DropDownControl == null)
                {
                    popupMenu = new PopupMenu(this.Parent.Parent.Parent.RibbonControl.Manager);
                    BarButtonItem.DropDownControl = popupMenu;
                }
                else
                    popupMenu = (PopupMenu)BarButtonItem.DropDownControl;

                BarButtonItem subMenu = new BarButtonItem();
                subMenu.Caption = Caption;
                subMenu.Glyph = icon;

                popupMenu.AddItem(subMenu);

                return new SubMenu(this, subMenu);
            }

            public Menu SetVisibleFor(int yetkiId)
            {
                bool allowed = Session.CurrentUser.YetkiliMi(yetkiId);

                this.BarButtonItem.Visibility = allowed ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.BarButtonItem.Links[0].Visible = allowed;

                return this;
            }

            public Menu Hide()
            {
                bool allowed = Session.CurrentUser.isAdmin;

                this.BarButtonItem.Visibility = allowed ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.BarButtonItem.Links[0].Visible = allowed;
                
                return this;
            }

            public void SetInvisibleForDepartments(params int[] Departments)
            {
                bool disallowed = false;
                foreach (int Dept in Departments)
                    disallowed |= Session.CurrentUser.DepartmentId == Dept;

                this.BarButtonItem.Visibility = disallowed ? BarItemVisibility.Never : BarItemVisibility.Always;
                this.BarButtonItem.Links[0].Visible = !disallowed;
            }

            public void BeginGroup()
            {
                foreach (BarItemLink bi in this.Parent.RibbonPageGroup.ItemLinks)
                {
                    if (bi.Item == this.BarButtonItem)
                    {
                        bi.BeginGroup = true;
                        return;
                    }
                }
            }
        }

        public class SubMenu
        {
            private Menu parent;
            public Menu Parent
            {
                get { return parent; }
                set { parent = value; }
            }

            private BarButtonItem barButtonItem;

            public BarButtonItem BarButtonItem
            {
                get { return barButtonItem; }
                set { barButtonItem = value; }
            }

            public SubMenu(Menu parent, BarButtonItem barButtonItem)
            {
                this.BarButtonItem = barButtonItem;
                this.Parent = parent;
            }

            public void AddCustomAction(Action Method)
            {
                this.BarButtonItem.ItemClick += delegate(object sender, ItemClickEventArgs e)
                {
                    Method();
                };
            }

            public SubMenu SetVisibleFor(int yetkiId)
            {
                bool allowed = Session.CurrentUser.YetkiliMi(yetkiId);

                this.BarButtonItem.Visibility = allowed ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.BarButtonItem.Links[0].Visible = allowed;

                return this;
            }

            public void Hide()
            {
                bool allowed = Session.CurrentUser.isAdmin;

                this.BarButtonItem.Visibility = allowed ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.BarButtonItem.Links[0].Visible = allowed;
            }
        }
        #endregion
    }


}
